define(['jquery', 'bootstrap', 'backend', 'table', 'form','layer','echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form,Layer,Echarts) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'crm/Statistics/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',
                },
                queryParams: function (params) {
                    // 自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    //filter.后跟的是在ajax里使用的名称
                    if($('#team').find('.active').find('a').attr('data-value')){
                        params['team'] = $('#team').find('.active').find('a').attr('data-value');
                    }
                    params['wf_group_id'] = $('#group').val();
                    params['wf_admin_id'] = $('#admin_id').val();
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange',visible:false},
                        {field: 'allot_date', title: __('Allot_date'), visible:false,operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'product', title: __('Product'),visible:false,operate: 'like'},
                    ]
                ]
            });
           var  EchartsModel=Echarts;
           var myChart1 = EchartsModel.init(document.getElementById('option1'));
           var myChart2 = EchartsModel.init(document.getElementById('option2'));
           var myChart3 = EchartsModel.init(document.getElementById('option3'));
           function ready() {
               myChart1.showLoading();
               myChart2.showLoading();
               myChart3.showLoading();
           }
           ready();
            function EchartsData(typeUserArray,statusUserCountArray,productArray) {
                //有效用户
                var option1 = {
                    title: {
                        text: '用户统计',
                        subtext: '真实数据',
                        left: 'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    toolbox: {
                        show: true,
                        orient: 'vertical',
                        left: 'right',
                        top: 'center',
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    series : [
                        {
                            type: 'pie',
                            radius : '65%',
                            center: ['50%', '50%'],
                            selectedMode: 'single',
                            data:[
                                {
                                    value:typeUserArray[0],
                                    name: '有效用户',
                                },
                                {
                                    value:typeUserArray[1],
                                    name: '无效用户',

                                }
                            ],

                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                },
                                normal:{
                                    color:function(params) {
                                        //自定义颜色
                                        var colorList = [
                                            '#60C0DD','#FE8463','#FCCE10','#E87C25','#27727B'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            }
                        }
                    ]
                };
                var option2 = {
                    title: {
                        text: '客户状态统计',
                        subtext: '真实数据',
                        left: 'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    toolbox: {
                        show: true,
                        orient: 'vertical',
                        left: 'right',
                        top: 'center',
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    series : [
                        {
                            type: 'pie',
                            radius : '65%',
                            center: ['50%', '50%'],
                            selectedMode: 'single',
                            data:[
                                {
                                    value:statusUserCountArray[0],
                                    name: '无意向客户'
                                },
                                {
                                    value:statusUserCountArray[1],
                                    name: '意向客户'
                                },
                                {
                                    value:statusUserCountArray[2],
                                    name: '谈合作'

                                },
                                {
                                    value:statusUserCountArray[3],
                                    name: '我方盖章'

                                },
                                {
                                    value:statusUserCountArray[4],
                                    name: '对方盖章'

                                },{
                                    value:statusUserCountArray[5],
                                    name: '待付款'

                                },{
                                    value:statusUserCountArray[6],
                                    name: '已签约'

                                }
                            ],

                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                },
                                normal:{
                                    color:function(params) {
                                        //自定义颜色
                                        var colorList = [
                                            '#60C0DD','#FE8463','#FCCE10','#E87C25','#27727B'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            }
                        }
                    ]
                };
                /*var option3 = {
                    title: {
                        text: '产品跟踪人数',
                        subtext: '真实数据'
                    },
                    tooltip : {
                        trigger: 'axis',
                        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                    },
                    toolbox: {
                        show: true,
                        orient: 'vertical',
                        left: 'right',
                        top: 'center',
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    xAxis: {
                        data: productArray['name'],
                        axisLabel: {
                            inside: true,
                            textStyle: {
                                color: '#fff'
                            }
                        },
                        axisTick: {
                            show: true
                        },
                        axisLine: {
                            show: true
                        },
                        z: 10
                    },
                    yAxis: {
                        axisLine: {
                            show: true
                        },
                        axisTick: {
                            show: true
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#999'
                            }
                        }
                    },
                    dataZoom: [
                        {
                            type: 'inside'
                        }
                    ],
                    series: [
                        {
                            type: 'bar',
                            name:'销售人数',
                            itemStyle: {
                                normal:{
                                 /!*   lineStyle:{
                                        color:'#FE8463'
                                    },*!/
                                    color:function(params) {
                                        //自定义颜色
                                        var colorList = [
                                            '#60C0DD','#FE8463','#FCCE10','#E87C25','#27727B'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            },
                            data: productArray['count'],
                        },
                        {
                            type: 'bar',
                            name:'B端客户人数',
                            itemStyle: {
                                normal:{
                                    /!*lineStyle:{
                                        color:'#60C0DD'
                                    },*!/
                                    color:function(params) {
                                        //自定义颜色
                                        var colorList = [
                                            '#60C0DD','#FE8463','#FCCE10','#E87C25','#27727B'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            },
                            data: productArray['usercount'],
                        }
                    ]
                };*/
                var option3 = {
                    title : {
                        text: '产品跟踪数据',
                        subtext: '真实数据'
                    },
                    tooltip : {
                        trigger: 'axis',
                        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                 /*   tooltip : {
                        trigger: 'axis',
                        formatter: function (params) {
                            var relVal = params[0].name;
                            for (var i = 0, l = params.length; i < l; i++) {
                                relVal += '<br/>' + params[i].seriesName + ' : ' + params[i].value + "条";
                            }
                            return relVal;
                        }
                    },*/
                    legend: {
                        data:['B端客户人数','签约客户人数']
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            dataView : {show: true, readOnly: false},
                            magicType : {show: true, type: ['line', 'bar']},
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : true,
                    xAxis : [
                        {
                            type : 'category',
                            data : productArray['name']
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value',
                            name : '人',
                            axisLabel:{
                                formatter:'{value}'
                            }
                        }
                    ],
                    series : [
                        {
                            name:'B端客户人数',
                            type:'bar',
                            data:productArray['userCount'],
                            itemStyle:{
                                normal:{
                                    color:'#FF99CC'
                                }
                            }
                            }
                        ,
                        {
                            name:'签约客户人数',
                            type:'bar',
                            data:productArray['signCount'],
                            itemStyle:{
                                normal: {
                                    color:'#66CCFF'
                                }
                            },
                            markPoint : {
                               /* data : [
                                    {name : '年最高', value : 182.2, xAxis: 7, yAxis: 183},
                                    {name : '年最低', value : 2.3, xAxis: 11, yAxis: 3}
                                ]*/
                            },
                            markLine : {
                            /*    data : [
                                    {type : 'average', name : '平均值'}
                                ]*/
                            },
                        },
                    ]
                };;
                // 使用刚指定的配置项和数据显示图表。
                myChart1.clear();
                myChart2.clear();
                myChart3.clear();
                myChart1.setOption(option1);
                myChart2.setOption(option2);
                myChart3.setOption(option3);
                myChart1.hideLoading();
                myChart2.hideLoading();
                myChart3.hideLoading();
            }
            // 为表格绑定事件
            Table.api.bindevent(table);
            //这里需要手动为Form绑定上元素事件
            Form.api.bindevent($("form#cxselectform"));
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                EchartsData(data['typeUserArray'],data['statusUserCountArray'],data['productArray']);
                //这里我们手动设置底部的值
            });
            $('.btn-success').click(function () {
                myChart1.showLoading();
                myChart2.showLoading();
                myChart3.showLoading();
            })
        },
        //网贷分析
        platform:function () {
            var  EchartsModel=Echarts;
            var myChart1 = EchartsModel.init(document.getElementById('option1'));
            Table.api.init({
                extend: {
                    index_url: 'crm/Statistics/platform',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',

                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                columns: [
                    [
                        {field: 'product', title: __('PlanName')},
                        {field: 'type', title: __('Type'),searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

            /**
             * 页面初始化
             */
            function ready() {
                myChart1.showLoading();
            }
            ready();

            /**
             * 封装Echarts
             * @param data
             */
            function option(data) {
                var option = {
                    title: {
                        text: '网贷客户识别',
                        subtext: '真实数据',
                        left: 'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    toolbox: {
                        show: true,
                        orient: 'vertical',
                        left: 'right',
                        top: 'center',
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    series : [
                        {
                            type: 'pie',
                            radius : '65%',
                            center: ['50%', '50%'],
                            selectedMode: 'single',
                            data:[
                                {
                                    value:data[0],
                                    name: '无效网贷',
                                },
                                {
                                    value:data[1],
                                    name: '有效网贷',

                                }
                            ],
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                },
                                normal:{
                                    color:function(params) {
                                        //自定义颜色
                                        var colorList = [
                                            '#60C0DD','#FE8463','#FCCE10','#E87C25','#27727B'
                                        ];
                                        return colorList[params.dataIndex]
                                    }
                                }
                            }
                        }
                    ]
                };
                myChart1.clear();
                myChart1.setOption(option);
                myChart1.hideLoading();
            }
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                option(data['countArray']);
                //这里我们手动设置底部的值
            });
        },
        buserfollow_count: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'crm/Statistics/buserfollow_count',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',
                },
                queryParams: function (params) {
                    // 自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    //filter.后跟的是在ajax里使用的名称
                    if($('#team').find('.active').find('a').attr('data-value')){
                        params['team'] = $('#team').find('.active').find('a').attr('data-value');
                    }
                    filter['wf_group_id'] = $('#group_id').val();
                    filter['wf_admin_id'] = $('#admin_id').val();
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'fastcount', title: __('Fastcount'),sortable: true},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('按钮组'),
                            table: table,
                            events: Table.api.events.operate,
                            operate:false,
                            buttons: [
                                {
                                    name: '跟进记录',
                                    text: __('跟进记录'),
                                    title: __('跟进记录'),
                                    extend:'title="跟进记录"',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-danger',
                                    icon: 'fa fa-pencil-square-o',
                                    url: 'crm/buserfolloe/index/type/2',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                    ]
                ],
                onLoadSuccess: function (data) {
                    // 在表格第次加载成功后,刷新左侧菜单栏彩色小角标,支持一次渲染多个
                    // 如果需要在进入后台即显示左侧的彩色小角标,请使用服务端渲染方式,详情修改application/admin/controller/Index.php
                    Backend.api.sidebar({
                        'example/colorbadge': data.total
                    });
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //这里需要手动为Form绑定上元素事件
            Form.api.bindevent($("form#cxselectform"));
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                Layer.closeAll('loading'); //关闭加载层
                $('#fastcount').text(data['totalArray'][0])
                //这里我们手动设置底部的值
            });
            /**
             * 排序
             */
            $('.sortable').click(function () {
                Layer.load(2, {shade: [0.2, '#eeeeee']});
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});